﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Collections;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Web.SessionState;

namespace ZhiAo.CoreLibrary.Common
{
    public class PageHelper
    {
        public static string ServerPath
        {
            get
            {
                return CurrentPage.Server.MapPath("~");
            }
        }

        public static HttpRequest CurrentRequest
        {
            get
            {
                Page page = (Page)HttpContext.Current.Handler;
                return page.Request;
            }
        }

        public static HttpResponse CurrentResponse
        {
            get
            {
                Page page = (Page)HttpContext.Current.Handler;
                return page.Response;
            }
        }

        public static Page CurrentPage
        {
            get
            {
                Page page = (Page)HttpContext.Current.Handler;
                return page;
            }
        }

        public static HttpSessionState Session
        {
            get { return System.Web.HttpContext.Current.Session; }
        }

        public static string Request(string key)
        {
            return CurrentRequest[key] == null ? "" : CurrentRequest[key];
        }

        public static void BindDropDownList(DropDownList drp, object dt, string sValueCol, string sTextCol, bool isLoadAll)
        {
            //是否考虑建立基础表 类似于放大镜
            //思考 如何给其加载数据  动态查询  OR   .LoadData()方法 扩展控件方法
        }

        public static bool LoadSearchCondition()
        {
            Page currentPage = (Page)HttpContext.Current.Handler;
            string pageId = currentPage.GetType().Name;
            if (IsHeadPage) pageId += Request("tablename");

            if (currentPage.Session[pageId] != null)
            {
                Hashtable hstable = (Hashtable)currentPage.Session[pageId];
                string controlId = "";
                foreach (DictionaryEntry _Entry in hstable)
                {
                    controlId = _Entry.Key.ToString();
                    Control control = currentPage.FindControl(controlId);
                    if (control != null)
                    {
                        if (control is TextBox)
                        {
                            ((TextBox)control).Text = _Entry.Value.ToString();
                        }
                        else if (control is DropDownList)
                        {
                            ((DropDownList)control).SelectedValue = _Entry.Value.ToString();
                        }
                        else if (control is CheckBox)
                        {
                            ((CheckBox)control).Checked = true;
                        }
                        else if (control is HiddenField)
                        {
                            ((HiddenField)control).Value = _Entry.Value.ToString();
                        }
                        //else if (_Control.GetType().BaseType.FullName == "Apps_JKS_ASCX_ECIChoose")
                        //{
                        //    ((R40TextBox)_Control.Controls[0]).Text = _Entry.Value.ToString();
                        //}
                    }
                }
                return true;
            }
            return false;
        }

        public static void SaveSearchCondition(Control searchTable)
        {
            Page currentPage = (Page)HttpContext.Current.Handler;
            Hashtable hstable = new Hashtable();
            foreach (Control subControl in searchTable.Controls)
            {
                SaveSearchCondition(subControl, hstable);
            }

            HtmlInputHidden hidden = currentPage.FindControl("yy_ECIGridView_y") as HtmlInputHidden;
            if (hidden != null)
            {
                hstable.Add(hidden.ID, hidden.Value);
            }

            string pageId = currentPage.GetType().Name;
            if (IsHeadPage) pageId += Request("tablename");
            currentPage.Session[pageId] = null;

            if (hstable.Count > 0)
            {
                currentPage.Session[pageId] = hstable;
            }
        }

        private static void SaveSearchCondition(Control container, Hashtable hstable)
        {
            Page _Page = (Page)HttpContext.Current.Handler;
            string filter = "";
            foreach (Control subControl in container.Controls)
            {
                SaveSearchCondition(subControl, hstable);

                filter = "";
                if (subControl is TextBox)
                {
                    if (((TextBox)subControl).Text.Trim() != "")
                    {
                        filter = ((TextBox)subControl).Text.Trim();
                    }
                }
                else if (subControl is HiddenField)
                {
                    if (((HiddenField)subControl).Value.Trim() != "")
                    {
                        filter = ((HiddenField)subControl).Value.Trim();
                    }
                }
                else if (subControl is DropDownList)
                {
                    if (((DropDownList)subControl).SelectedValue != "")
                    {
                        filter = ((DropDownList)subControl).SelectedValue;
                    }
                }
                else if (subControl is CheckBox)
                {
                    if (((CheckBox)subControl).Checked)
                    {
                        filter = "1";
                    }
                }
                //else if ((_Control.GetType().BaseType.FullName == "Apps_JKS_ASCX_ECIChoose") && (((R40TextBox)_Control.Controls[0]).Text.Trim() != ""))
                //{
                //    strFilt = ((R40TextBox)_Control.Controls[0]).Text.Trim();
                //}

                if (filter != "")
                {
                    hstable.Add(subControl.ID, filter);
                }
            }

        }

        public static bool IsDetailPage
        {
            get
            {
                string requestUrl = PageHelper.CurrentRequest.Url.ToString();
                return requestUrl.ToLower().Contains("TableRecord.aspx".ToLower());
            }
        }

        public static bool IsHeadPage
        {
            get
            {
                string requestUrl = PageHelper.CurrentRequest.Url.ToString();
                return requestUrl.ToLower().Contains("TableRecordList.aspx".ToLower());
            }
        }

        public static void RegisterHiddenField(string name, string value)
        {
            Page page = (Page)HttpContext.Current.Handler;
            page.ClientScript.RegisterHiddenField(name, value);
        }

        public static string ReturnParameter(string url)
        {
            //移动到CMN中
            string pageId = PageHelper.CurrentPage.Page.ClientID;
            if (PageHelper.Request("tablename") != "") pageId += "_" + PageHelper.Request("tablename");

            if (PageHelper.CurrentPage.Session[pageId + "_ReturnParameter"] == null)
            {
                return url;
            }
            else
            {
                return url + PageHelper.CurrentPage.Session[pageId + "_ReturnParameter"].ToString();
            }
        }
    }
}
